import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_lib/utils/DpUtils.dart';
import 'package:flutter_my_web/constants/ConstantsUrlImage.dart';
import 'package:flutter_my_web/pages/files/filePage_controller.dart';
import 'package:flutter_my_web/pages/files/file_page_item_widget.dart';
import 'package:flutter_my_web/utils/Utils.dart';
import 'package:flutter_my_web/widget/AppBarTitleWidget.dart';
import 'package:flutter_my_web/widget/ImageWidget.dart';
import 'package:flutter_my_web/widget/TextWidget.dart';
import 'package:get/get.dart';

class FilePage extends StatefulWidget {
  const FilePage({Key? key}) : super(key: key);

  @override
  State<FilePage> createState() => _FilePageState();
}

class _FilePageState extends State<FilePage> {
  var logic = FilePageController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          elevation: 0,
          title: GestureDetector(
            onTap: () {},
            child: AppBarTitleWidget("文件夹"),
          ),
          centerTitle: true,
          actions: [
            ImageIconButton(ConstantsUrlImage.iconUpdate, Size(20,20), () async {
              var retPaths =
                  await FilePicker.platform.pickFiles(allowMultiple: true);
              if(retPaths==null||retPaths.files.length==0){
                showSnackBar("没有选择文件");
                return;
              }
              List<File> files = [];
              retPaths.files.forEach((element) { 
                files.add(File(element.path!));
              });
              logic.updateFiles(files);
            }),
          ],
          leading: IconButton(
              icon: Icon(
                Icons.arrow_back,
                color: Colors.white,
              ),
              onPressed: () {
                Get.back();
              })),
      body: GetBuilder<FilePageController>(
        init: logic,
        builder: (_Logic) {
          return SingleChildScrollView(
            child: Column(
              crossAxisAlignment:CrossAxisAlignment.start,
              children: [
                SizedBox(height: 10.0.hDp,),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Row(
                    children: [
                      SimpleText(
                        "当前目录：",
                        fontSize: 20.0.sp,
                        fontWeight: FontWeight.bold,
                        color: Colors.black,
                      ),
                      SimpleSelectableText(
                        "${logic.curPath}",
                        color: Colors.black,
                        fontSize: 16.0.sp,
                      ),
                      Expanded(child: SizedBox()),
                      ImageIconButton(ConstantsUrlImage.iconBack, Size(20,20), (){
                        logic.backDirectory();
                      }),
                    ],
                  ),
                ),
                SizedBox(height: 10.0.hDp,),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Wrap(
                    spacing: 10.0.wDp,
                    runSpacing: 10.0.wDp,
                    alignment: WrapAlignment.start,
                    children: logic.list.map((e) => InkWell(onTap:(){
                      logic.itemAction(e);
                    },child: FilePageItemWidget(e))).toList(),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
